from kivy.graphics import Color, Line, Rectangle
from kivy.properties import ListProperty, NumericProperty, StringProperty
from kivy.uix.button import Button

from kivy_cust.ConfigComm import ConfigComm

class ButtonComm(Button):
    font_size = NumericProperty(18)
    color = ListProperty((0, 0, 0, 1))  # 设置文字颜色
    # background_color = ListProperty((0.529, 0.808, 0.922, 0.7))  # 按钮背景颜色（如果不使用Property设置默认样式，kv文件调用时无法覆盖默认样式）
    # background_normal = ''  # 按钮未点击时背景色（如果不把该项设为空，background_color颜色显示会深一些）
    background_normal = StringProperty(ConfigComm.IMG_BUTTON_COMM)
    background_down = StringProperty(ConfigComm.IMG_BUTTON_COMM_PRESSED)

    def __init__(self, **kwargs):
        super(ButtonComm, self).__init__(**kwargs)
        self.size_hint = (None, None)
        self.size = (100, 35)

        with self.canvas:
            # 设置边框
            Color(0, 0.8, 0.8, .5)
            self.border_line = Line(rectangle=(self.x, self.y, self.width, self.height), width=1)

        # 更新矩形和边框的大小和位置
        self.bind(size=self.update_rect, pos=self.update_rect)

    def update_rect(self, *args):
        # 更新边框
        self.border_line.rectangle = (self.x, self.y, self.width, self.height)
